package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import model.AppModel;
import view.AboutCoolMail;
import view.AppView;

/**
 * Controller for the main menu of the application
 * @author MUSAB
 *
 * @version $Revision: 1.0 $
 */
public class MenuController {

	// Application model and view
	private AppModel model;
	private AppView view;
	private AppController appController;
	
	/**
	 * Constructor for MenuController.
	 * @param model AppModel
	 * @param view AppView
	 * @param appController AppController
	 */
	public MenuController(AppModel model, AppView view, AppController appController) {
		this.model = model;
		this.view = view;
		this.appController = appController;
		assignHandlersToEvents();
	}

	private void assignHandlersToEvents() {
		
		//Registration of handlers
		//Exit 
		view.getMainMenu().getExitMenuItem().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				actionExit(e);
			}
		});
		
		//About 
		view.getMainMenu().getAboutMenuItem().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				actionAbout(e);
			}
		});
		
		// Templates
		view.getMainMenu().getTemplatesMenuItem().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				actionTemplates();
			}
		});
		
		// Tokens
		view.getMainMenu().getTokensMenuItem().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				actionTokens();
			}
		});
		
		// Meetings
		view.getMainMenu().getMeetingsMenuItem().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				actionMeetings();
			}
		});
		
		
		// Filters
		view.getMainMenu().getFiltersMenuItem().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				actionFilters();
			}
		});
		
	}

	/**
	 * Launches the filters controller which manages the filters
	 */
	protected void actionFilters() {
		new FiltersViewController(model);
		appController.getNavigationController().refreshNavigationFolders("inbox");
	}

	/**
	 * Launches the meetings view and its controller
	 */
	protected void actionMeetings() {
		new MeetingsController(model);
		appController.getNavigationController().refreshNavigationFolders("inbox");
	}

	/**
	 * Launches the tokens view and its controller
	 */
	protected void actionTokens() {
		new TokenViewController(model);
		
	}

	/**
	 * Launches the template view and its controller
	 */
	protected void actionTemplates() {
		new TemplateViewController(model);
	}

	/**
	 * Developers information
	 * @param e ActionEvent
	 */
	protected void actionAbout(ActionEvent e) {
		  AboutCoolMail aboutBox = new AboutCoolMail();
		    
		    aboutBox.setVisible( true );
		
	}

	/**
	 * Method actionExit.
	 * @param e ActionEvent
	 */
	protected void actionExit(ActionEvent e) {
		System.exit( 0 );
		
	}

}
